home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_315 / surf / mapimgpix.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  128 lines

  1. #include "mytypes.h"
  2. #include "readilbm.h"
  3.  
  4. #define DefRes 2
  5. int MapImageV = DefRes*DefRepV,
  6.     MapImageH= DefRes*DefRepH; /* virtual screen size */
  7. static int PixV=DefRes,
  8.            PixH=DefRes;  /* true ilbm size in pixels */
  9. short MapRepV = DefRepV,
  10.       MapRepH = DefRepH;
  11. static short BytesPerLine;
  12. static unsigned char *Raster= null;
  13. static long MaxShade;
  14. static bool AxisFlipped = DefXYFlip;
  15. /*
  16.  * Update the MapImageH and MapImageV variables
  17.  */
  18. void PrepImgPix()
  19. {
  20.     MapImageV = PixV * MapRepV;
  21.     MapImageH = PixH * MapRepH;
  22.     if( AxisFlipped ) {
  23.         int temp;
  24.         temp = MapImageV;
  25.         MapImageV = MapImageH;
  26.         MapImageH = temp;
  27.     }
  28. }
  29. /*
  30.  * free up any memory holding mapping image
  31.  */
  32. void CloseImgPix()
  33. {
  34.         if( Raster) free(Raster);
  35.         Raster = null;
  36.         PixV = 0xff; PixH = 0xff;
  37.         PrepImgPix();
  38. }
  39.  
  40. /*
  41.  * cause x and y axises to be reversed
  42.  */
  43. void FlipImgPix( flip )
  44.     bool flip;
  45. {
  46.     AxisFlipped = flip;
  47.     PrepImgPix();
  48. }
  49.  
  50. /*
  51.  * 4 bits per pixel means 2 pixels per byte
  52.  */
  53. bool OpenImgPix(sizex, sizey, maxshade)
  54.     int sizex, sizey;
  55.     short maxshade;
  56. {
  57.     CloseImgPix();
  58.     if( maxshade == 0 ) {
  59.         OutErr("OpenImgPix: got max shade = 0\n");
  60.         maxshade = 1;
  61.      }
  62.     MaxShade = maxshade;
  63.     BytesPerLine = (sizex +1)/2;
  64.     Raster = (unsigned char *) malloc( BytesPerLine * sizey);
  65.     if( !Raster ) {
  66.         printf("OpenImgPix: not enough memory\n");
  67.         return(false); /* no memory err */
  68.     }
  69.  
  70.     PixV = sizey;
  71.     PixH = sizex;
  72.     PrepImgPix();
  73.     return(true);
  74. }
  75.  
  76. #define CalcByte(cbx,cby) (Raster + ( BytesPerLine * cby ) + (cbx >> 1))
  77.  
  78.  
  79. void SetImgPix(x, y, val)
  80.     int x, y; /* location */
  81.     int val;
  82. {
  83.     unsigned char *bite;
  84.     unsigned char shade;
  85.  
  86.     if( x > PixH || x < 0 || y > PixV || y < 0 ) {
  87.         OutErr("SetImgPix(%d,%d,%d) out of range\n",x,y,val);
  88.         return;
  89.     }
  90.  
  91.     if( !Raster) return;
  92.     shade = ( (val<< 4)-val)/MaxShade;
  93.     bite = CalcByte(x,y);
  94.     if( x & 1) {
  95.         *bite = (*bite & 0xf) | ( shade <<4 );
  96.     }
  97.     else {
  98.         *bite = (*bite & 0xf0) | shade;
  99.     }
  100. }
  101.  
  102.  
  103. short GetImgPix(x,y)
  104.     int x, y;
  105. {
  106.     unsigned char *bite;
  107.  
  108.     if( AxisFlipped ) {
  109.         int temp;
  110.         temp = x; x = y; y = temp;
  111.     }
  112.  
  113.     x %= PixH;
  114.     y %= PixV;
  115.  
  116.     if( !Raster ) {
  117.         return( (short)(((x ^ y)& 0x10) ? 0xff: 0));
  118.     }
  119.     bite = CalcByte(x,y);
  120.  
  121.     if( x & 1) {
  122.         return((short)(*bite & 0xf0));
  123.     }
  124.     else {
  125.         return( (short)((*bite & 0x0f) <<4));
  126.     }
  127. }
  128.